package com.ph.elec.gwt.register.client.ui;

import java.util.Date;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.ph.elec.gwt.register.client.OptionLists;
import com.ph.elec.gwt.register.client.ValidationUtils;
import com.ph.elec.gwt.util.UIUtils;
import com.ph.elec.gwt.util.UIUtils.StdDecoratorPanel;

public class PersonalInfoPanel extends StdDecoratorPanel {
	
	public TextBox txtFirstName = new TextBox();
	
	public TextBox txtMiddleName = new TextBox();
	public TextBox txtLastName = new TextBox();

	public ListBox lstBdayMonth = new ListBox();
	public TextBox txtBdayDay = new TextBox();
	public TextBox txtBdayYear = new TextBox();
	public TextBox txtAge = new TextBox();

	public ListBox lstCivilStatus = new ListBox();
	public ListBox lstEducation = new ListBox();

	public ListBox lstGender = new ListBox();
	public ListBox lstOccupation = new ListBox();
	public TextBox txtNumChild = new TextBox();
	
	public PersonalInfoPanel() {
		vtpRoot.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtFirstName, "250px", 30), 
				"First Name", 
				true));

		HorizontalPanel hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtMiddleName, "125px", 20), 
				"Middle Name", 
				true));
		
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtLastName, "150px", 25), 
				"Last Name", 
				true));
		vtpRoot.add(hzpRow);

		hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstBdayMonth, OptionLists.MONTHS), 
						"125px"), 
				"Birthday", 
				true));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtBdayDay, "20px", 2, TextBox.ALIGN_RIGHT), 
				"Day", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtBdayYear, "35px", 4, TextBox.ALIGN_RIGHT), 
				"Year", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtAge, "40px", 4, TextBox.ALIGN_RIGHT), 
				"Age", 
				false));
		txtAge.setEnabled(false);
		vtpRoot.add(hzpRow);
		
		hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstCivilStatus, OptionLists.CIVIL_STATUS), 
						"125px"), 
				"Civil Status", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstOccupation, OptionLists.OCCUPATION),
						"320px"), 
				"Occupation", 
				false));
		vtpRoot.add(hzpRow);

		hzpRow = new HorizontalPanel();
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstGender, OptionLists.GENDER),
						"125px"), 
				"Gender", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateWidth(
						UIUtils.decorateListBox(lstEducation, OptionLists.EDUC_ATTAIN),
						"221px"), 
				"Educational Attainment", 
				false));
		hzpRow.add(UIUtils.decorateFieldName(
				UIUtils.decorateTextBox(txtNumChild, "80px", 2, TextBox.ALIGN_RIGHT), 
				"No. of Children", 
				false));
		vtpRoot.add(hzpRow);
		
		add(vtpRoot);
		setWidth("500px");
		
		initializeContents();
		initializeHandlers();
	}
	
	private void initializeContents() {
		txtBdayDay.setText("1");
		txtBdayYear.setText("1980");
	}

	protected void initializeHandlers() {
		// Age Update Mechanism
		BirthdayChangeHandler birthdayChangeHandler = new BirthdayChangeHandler();
		txtBdayYear.addChangeHandler(birthdayChangeHandler);
		txtBdayDay.addChangeHandler(birthdayChangeHandler);
		lstBdayMonth.addChangeHandler(birthdayChangeHandler);
	}
	
	@SuppressWarnings("deprecation")
	protected String checkBirthdayText(String str_year, int month, String str_day) {
		StringBuffer message = new StringBuffer();
		int year = 0;
		try {
			year = Integer.parseInt(str_year);
			if (year < 1900 || year > 2010) {
				message.append("Birthday Year is not possibly correct.\n");
			}
		} catch (NumberFormatException e) {
			message.append("Birthday Year must be a number.  (ex: 1993)\n");
		}
		int day = 0;
		try {
			day = Integer.parseInt(str_day);
			if (day < 1 || day > 31) {
				message.append("Birthday Day is incorrect.\n");
			}
		} catch (NumberFormatException e) {
			message.append("Birthday Day must be a number.  (ex: 13)\n");
		}
		if (month == 0) {
			message.append("Birthday Month must be selected.\n");
		}
		if (message.length() > 0) {
			return message.toString();
		}
		
		try {
			year -= 1900;
			Date bday = new Date(year, month - 1, day);
			
			if (ValidationUtils.checkBirthday(bday) != null) {
				message.append("Birthday Year could be incorrect or Birthday is wrong.\n");
			}
		} catch (Exception e) {
			message.append("Birthday Day or Year is wrong.\n");
		}		
		return message.toString();
	}
	
	private class BirthdayChangeHandler implements ChangeHandler {
		
		boolean invalid = true;

		@SuppressWarnings("deprecation")
		public void onChange(ChangeEvent event) {
			invalid = false;
			
			String message = checkBirthdayText(txtBdayYear.getText(), 
					lstBdayMonth.getSelectedIndex(), 
					txtBdayDay.getText());
			if (message.contains(" Day")) {
				invalidateDay();
			}
			if (message.contains(" Year")) {
				invalidateYear();
			}
			if (message.length() == 0) {
				txtBdayDay.removeStyleName("invalid");	
				txtBdayYear.removeStyleName("invalid");
				Date bday = new Date(Integer.valueOf(txtBdayYear.getText()) - 1900, 
						lstBdayMonth.getSelectedIndex() - 1,
						Integer.valueOf(txtBdayDay.getText()));
				txtAge.setText(String.valueOf(ValidationUtils.getAge(bday)));
			} else {
				txtAge.setText("");
			}
		}

		private void invalidateYear() {
			invalid = true;
			txtBdayYear.addStyleName("invalid");				
		}

		private void invalidateDay() {
			invalid = true;
			txtBdayDay.addStyleName("invalid");				
		}
	}

	public boolean validate() {
		StringBuffer message = new StringBuffer();
		
		message.append(ValidationUtils.checkName(txtFirstName.getText(), 
				txtMiddleName.getText(), 
				txtLastName.getText()));
		message.append(checkBirthdayText(txtBdayYear.getText(), 
					lstBdayMonth.getSelectedIndex(), 
					txtBdayDay.getText()));
		
		if (message.length() > 0) {
			addMultipleErrorMessage(message.toString());
			return false;
		}
		return true;
	}
}
